package MEET.src.View;

import MEET.src.code.UserDao;

import javax.swing.*;

public class Register extends JFrame {
    // 定义表单控件
    private JTextField usernameField;
    private JTextField IDField;
    private JPasswordField passwordField;
    private JTextField ageField;
    private JComboBox<String> genderComboBox;
    private JComboBox<String> areaComboBox;
    private JComboBox<String> identityComboBox;
    private JComboBox<String> departmentComboBox;
    private JComboBox<String> powerComboBox;

    public Register() {
        // 初始化注册界面组件
        initRegisterForm();
    }

    private void initRegisterForm() {
        setTitle("用户注册");
        setSize(500, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(null);

        //姓名
        JLabel usernameLabel = new JLabel("性名:");
        usernameLabel.setBounds(50, 10, 100, 30);
        add(usernameLabel);
        usernameField = new JTextField();
        usernameField.setBounds(150, 10, 200, 30);
        add(usernameField);

        // 用户名
        JLabel IDnameLabel = new JLabel("用户账号:");
        IDnameLabel.setBounds(50, 50, 100, 30);
        add(IDnameLabel);
        IDField = new JTextField();
        IDField.setBounds(150, 50, 200, 30);
        add(IDField);

        // 密码
        JLabel passwordLabel = new JLabel("密码:");
        passwordLabel.setBounds(50, 90, 100, 30);
        add(passwordLabel);
        passwordField = new JPasswordField();
        passwordField.setBounds(150, 90, 200, 30);
        add(passwordField);

        // 年龄
        JLabel ageLabel = new JLabel("年龄:");
        ageLabel.setBounds(50, 130, 100, 30);
        add(ageLabel);
        ageField = new JTextField();
        ageField.setBounds(150, 130, 200, 30);
        add(ageField);

        // 性别
        JLabel genderLabel = new JLabel("性别:");
        genderLabel.setBounds(50, 170, 100, 30);
        add(genderLabel);
        genderComboBox = new JComboBox<>(new String[] {"男", "女"});
        genderComboBox.setBounds(150, 170, 100, 30);
        add(genderComboBox);

        // 区域
        JLabel areaLabel = new JLabel("区域:");
        areaLabel.setBounds(50, 210, 100, 30);
        add(areaLabel);
        areaComboBox = new JComboBox<>(new String[] {"杨浦", "临港"});
        areaComboBox.setBounds(150, 210, 100, 30);
        add(areaComboBox);

        // 身份
        JLabel identityLabel = new JLabel("身份:");
        identityLabel.setBounds(50, 250, 100, 30);
        add(identityLabel);
        identityComboBox = new JComboBox<>(new String[] {"老师", "学生"});
        identityComboBox.setBounds(150, 250, 100, 30);
        add(identityComboBox);

        // 学院
        JLabel deptLabel = new JLabel("学院:");
        deptLabel.setBounds(50, 290, 100, 30);
        add(deptLabel);
        departmentComboBox = new JComboBox<>(new String[] {
                "经济与管理学院",
                "计算机科学与技术学院",
                "电子与信息工程学院",
                "电气工程学院",
                "外国语学院",
                "环境与化学工程学院",
                "数理学院",
                "自动化工程学院",
                "能源与机械工程学院"
        });
        departmentComboBox.setBounds(150, 290, 200, 30);
        add(departmentComboBox);

        // 权限
        JLabel powerLabel = new JLabel("权限:");
        powerLabel.setBounds(50, 330, 100, 30);
        add(powerLabel);
        powerComboBox = new JComboBox<>(new String[] {"普通用户", "管理员"});
        powerComboBox.setBounds(150, 330, 100, 30);
        add(powerComboBox);

        // 注册按钮
        JButton registerButton = new JButton("注册");
        registerButton.setBounds(150, 380, 100, 30);
        registerButton.addActionListener(e -> {
            if (validateForm()) {
                String ID=IDField.getText();
                String username = usernameField.getText();
                String password = new String(passwordField.getPassword());
                String age = ageField.getText();
                String gender = genderComboBox.getSelectedItem().toString();
                String area = areaComboBox.getSelectedItem().toString();
                String identity = identityComboBox.getSelectedItem().toString();
                String dept = departmentComboBox.getSelectedItem().toString();
                String power = powerComboBox.getSelectedItem().toString();

                boolean registered = UserDao.registerUser(
                        ID,
                        username,
                        password,
                        age,
                        gender,
                        area,
                        identity,
                        dept,
                        power
                );

                if (registered) {
                    JOptionPane.showMessageDialog(null, "注册成功！");
                    this.dispose();
                    // 跳转到登录页面
                    Login loginForm = new Login();
                    loginForm.setVisible(true);
                } else {
                    JOptionPane.showMessageDialog(null, "注册失败！");
                }
            }
        });
        add(registerButton);
    }

    private boolean validateForm() {
        if (usernameField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "用户名不能为空！");
            return false;
        }
        if (passwordField.getPassword().length == 0) {
            JOptionPane.showMessageDialog(null, "密码不能为空！");
            return false;
        }
        if (ageField.getText().isEmpty()) {
            JOptionPane.showMessageDialog(null, "年龄不能为空！");
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> new Register().setVisible(true));
    }
}